//
//  MainViewController.swift
//  0729FirstProgramMicroblog
//
//  Created by ding on 15/7/29.
//  Copyright © 2015年 ding. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewController("首页", imgName: "tabbar_home", vc: HomeTableViewController())
        addChildViewController("消息", imgName: "tabbar_message_center", vc: MessageTableViewController())
        addCenterView()
        addChildViewController("发现", imgName: "tabbar_discover", vc: DiscoverTableViewController())
        addChildViewController("我", imgName: "tabbar_profile", vc: ProfileTableViewController())
        
    }
    
//    func addChildViewController() {
//        let homeVc = HomeTableViewController()
//        homeVc.title = "首页"
//        homeVc.tabBarItem.image = UIImage(named: "tabbar_home")
//        
//        let nav = UINavigationController(rootViewController: homeVc)
//        
//        tabBar.tintColor = UIColor.orangeColor()
//        
//        addChildViewController(nav)
//    }
    
    func addChildViewController(titleName : String, imgName : String, vc : UIViewController) {
        
        vc.title = titleName
        vc.tabBarItem.image = UIImage(named: imgName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        tabBar.tintColor = UIColor.orangeColor()
        
        addChildViewController(nav)
    }

    func addCenterView() {
        let centerVc = UIViewController()
        let nav = UINavigationController(rootViewController: centerVc)
        centerVc.view.backgroundColor = UIColor.darkGrayColor()
        addChildViewController(nav)
        
    }

//    override func viewDidAppear(animated: Bool) {
//        super.viewDidAppear(animated)
//        let btn = UIButton(frame: CGRectMake(150, 0, 80, 48))
//        btn.backgroundColor = UIColor.redColor()
//        var normalImage = UIImage(named: "tabbar_compose_button")
//        normalImage = btn.backgroundImageForState(UIControlState.Normal)
//        tabBar.addSubview(btn)
//    }
    /// MARK: - 懒加载
    lazy private var composeBtn : UIButton = {
        let btn = UIButton()
        self.tabBar.addSubview(btn)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlited"), forState: UIControlState.Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlited"), forState: UIControlState.Highlighted)
        return btn
    }()

    func setupComposeBtn (){
        let btnW = tabBar.bounds.width / CGFloat((viewControllers?.count)!)
        let rect = CGRectMake(0, 0, btnW, tabBar.bounds.height)
        composeBtn.frame = CGRectOffset(rect, 2 * btnW, 0)
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        setupComposeBtn()
    }
}
